<?php

header("Content-type:application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=resultaten.xls");

require "vuelta.config.php";

function query_chosen($db, $account_id) {
  $query = "SELECT runner.name FROM account, chosen, runner
            WHERE account.id = '$account_id' AND 
            chosen.Account = account.id AND
            chosen.Runner = runner.id
            ORDER BY runner.name ASC;";
  $r = $db->query($query);
  if(!$r) { die("could not get chosen runners\n" . mysql_error()); }
  $ret = Array();
  while($row = mysql_fetch_row($r)){
    $ret[] = $row[0];
  }
  return $ret;
}

if(empty($_POST)) {
  ?><b>geen account id verstuurd</b><?php
}
elseif(pget('password') != '1234asdf') {
  ?><b>verkeerd wachtwoord</b><?php
}
else {
  $id = pget('id');
  $db = Vuelta::get_database_connection();
  $chosen = query_chosen($db, $id);
  $db->close();
?>

  <table>
    <tr>
      <td><b>gebruiker:</b></td>
      <td><?=$id?></td>
    </tr>
    <tr>
      <td><b>selectie</b></td>
    </tr>
  <?php
    for($i = 0; $i < 30; $i++) {
      $key = $i+1;
      $value = isset($chosen[$i]) ? $chosen[$i] : "";
  ?>
    <tr>
      <td><?=$key?></td>
      <td><?=$value?></td>
    </tr>
  <?php
    }
  ?>
  </table>

<?php
} // post
?>
